﻿using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using Srvtools;
using System.IO;
using System.Web;

public partial class EmployeeExport_Choose : System.Web.UI.Page
{
    protected void Page_Load()
    {
        if (!IsPostBack)
            Session["HrBack"] = Request.UrlReferrer.ToString();
    }

    string tempTable = @"
select 
    EmployeeID,EmpName,Birthday,IDCard,Major,School,Graduate,ContactPhone,Address,ContactPeople,ContactPeoplePhone,Accession,Dimission,Extension,Mobile,Email,IDCardAddress,PerRemark,HrRemark,Passport,Passdeadline,Residedeadline,Workdeadline,Regperres,ProbationerShip,
    D.DepartmentName,J.JobName,W.ValueName as WorkContractType,Jt.ValueName as JobType,S.ValueName as Sex,Es.ValueName as EmpStatus,C.ValueName as Country,Edu.ValueName as Education,T.ValueName as Title,
    C1.ValueName as CompetencePermit,C2.ValueName as CompetencePermit2,C3.ValueName as CompetencePermit3,Docent.ValueName as Shoulderdocent,F.ValueName as Framework
from (select * from Employees{0}) E
left join Department D on (E.departmentid=D.departmentid)
left join CompanyJob J on (E.JobId=J.JobId)
left join (select * from Employees_LinkValue where TypeId='JobType') Jt on (E.JobType=Jt.ValueId)
left join (select cast(case when ValueId = 'False' then 0 else 1 end as bit) as ValueId,ValueName from Employees_LinkValue where TypeId='Sex') S on (E.Sex=S.ValueId)
left join (select * from Employees_LinkValue where TypeId='EmpStatus') Es on (E.EmpStatus=Es.ValueId)
left join (select * from Employees_LinkValue where TypeId='Country') C on (E.Country=C.ValueId)
left join (select * from Employees_LinkValue where TypeId='Education') Edu on (E.Education=Edu.ValueId)
left join (select * from Employees_LinkValue where TypeId='Title') T on (E.Title=T.ValueId)
left join (select * from Employees_LinkValue where TypeId='CompetencePermit') C1 on (E.CompetencePermit=C1.ValueId)
left join (select * from Employees_LinkValue where TypeId='CompetencePermit') C2 on (E.CompetencePermit2=C2.ValueId)
left join (select * from Employees_LinkValue where TypeId='CompetencePermit') C3 on (E.CompetencePermit3=C3.ValueId)
left join (select cast(case when ValueId = 'False' then 0 else 1 end as bit) as ValueId,ValueName from Employees_LinkValue where TypeId='Shoulderdocent') Docent on (E.Shoulderdocent=Docent.ValueId)
left join (select * from Employees_LinkValue where TypeId='Framework') F on (E.Framework=F.ValueId)
left join (select * from Employees_LinkValue where TypeId='WorkContractType') W on (E.WorkContractType=W.ValueId)";

    protected void OnExport(object sender, EventArgs e)
    {
        StringBuilder fields = new StringBuilder();
        foreach (ListItem item in ChkChoose.Items)
        {
            if (item.Selected)
                fields.Append(item.Value + ",");
        }

        string f = fields.ToString().TrimEnd(',');
        string where = Server.UrlDecode(Request["where"]).Trim();

        string dir = Path.GetTempPath();
        string file = Path.Combine(dir, Guid.NewGuid().ToString("n") + ".xls");
        DataSet table = CliUtils.ExecuteSql("GLModule", "cmdDDUse", "select " + f + " from (" + string.Format(tempTable,string.IsNullOrEmpty(where) ? "" : (" where " + where)) + ") tempTable ", true, CliUtils.fCurrentProject);

        foreach (ListItem item in ChkChoose.Items)
        {
            if (item.Selected)
                table.Tables[0].Columns[item.Value].ColumnName = item.Text;
        }

        InfoDataSet set = new InfoDataSet();
        set.RealDataSet = table;
        set.ToExcel(0, file);

        try
        {
            FileInfo fi = new FileInfo(file);
            ResponseHelper.DownloadFile(HttpContext.Current.Response, fi, Request["module"] ?? fi.Name);
        }
        finally
        {
            File.Delete(file);
        }
    }

    protected void EmpBack_Click(object sender, EventArgs e)
    {
        string url = Session["HrBack"].ToString();
        Response.Redirect(url);
    }
}
